package com.ai.service.impl;

import com.ai.entity.AiProvider;
import com.ai.exception.BusinessException;
import com.ai.mapper.AiProviderMapper;
import com.ai.service.AiProviderService;
import com.ai.utils.ConvertUtils;
import com.ai.web.PageResp;
import com.ai.web.request.AiProviderAddReq;
import com.ai.web.request.AiProviderPageReq;
import com.ai.web.request.AiProviderUpdateReq;
import com.ai.web.response.AiProviderPageResp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class AiProviderServiceImpl implements AiProviderService {

    @Resource
    AiProviderMapper aiProviderMapper;

    @Override
    public PageResp<AiProviderPageResp> page(AiProviderPageReq aiProviderPageReq) {
        LambdaQueryWrapper<AiProvider> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(aiProviderPageReq.getProviderName())) {
            queryWrapper.like(AiProvider::getProviderName, aiProviderPageReq.getProviderName());
        }
        if (!StringUtils.isEmpty(aiProviderPageReq.getProviderEn())) {
            queryWrapper.gt(AiProvider::getProviderEn, aiProviderPageReq.getProviderEn());
        }
        IPage<AiProvider> iPage = aiProviderMapper.selectPage(new Page<>(aiProviderPageReq.getPage(), aiProviderPageReq.getLimit()), queryWrapper);
        return ConvertUtils.convertPageResp(iPage, e -> ConvertUtils.convert(e, AiProviderPageResp.class));
    }

    @Override
    public void addProvider(AiProviderAddReq aiProviderAddReq) {
        LambdaQueryWrapper<AiProvider> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AiProvider::getProviderEn, aiProviderAddReq);
        AiProvider aiProvider = aiProviderMapper.selectOne(queryWrapper);
        if (null != aiProvider) {
            throw new BusinessException("已经存在的模型供应商标识");
        }
        AiProvider aiProviderSave = new AiProvider();
        aiProviderSave.setProviderName(aiProviderAddReq.getProviderName());
        aiProviderSave.setProviderEn(aiProviderAddReq.getProviderEn());
        aiProviderSave.setProviderIcon(aiProviderAddReq.getProviderIcon());
        aiProviderMapper.insert(aiProviderSave);
    }

    @Override
    public void updateProvider(AiProviderUpdateReq aiProviderUpdateReq) {
        AiProvider aiProvider = aiProviderMapper.selectById(aiProviderUpdateReq.getId());
        if (null != aiProvider) {
            aiProvider.setProviderName(aiProviderUpdateReq.getProviderName());
            aiProvider.setProviderEn(aiProviderUpdateReq.getProviderEn());
            aiProvider.setProviderIcon(aiProviderUpdateReq.getProviderIcon());
            aiProviderMapper.updateById(aiProvider);
        }

    }

    @Override
    public void deleteProvider(List<String> providerIds) {
        aiProviderMapper.deleteBatchIds(providerIds);
    }

}
